'use strict';
const {getDefaultModelConfig} = require("../../utils/common");

module.exports = app => {
    const { STRING, INTEGER, TINYINT, DECIMAL, TEXT, BIGINT } = app.Sequelize;
    const Travel = app.model.define(
        'Travel',
        {
            id: {type: INTEGER, primaryKey: true, autoIncrement: true},
            name: {type: STRING(255), allowNull: false, comment: '旅行名称'},
            cover: {type: STRING(500), comment: '旅行封面图片'},
            images: {type: TEXT, comment: '旅行轮播图，JSON格式存储'},
            description: {type: TEXT, comment: '旅行详情'},
            notice: {type: TEXT, comment: '出行须知'},
            category_id: {type: INTEGER, comment: '旅行分类ID'},
            sort: {type: INTEGER, defaultValue: 0, comment: '排序'},
            status: {type: TINYINT, defaultValue: 1, comment: '状态：1-上架，0-下架'},
            is_virtual: {type: TINYINT, defaultValue: 0, comment: '是否虚拟旅行：1-是，0-否'},
            price_text: {type: DECIMAL(10, 2), comment: '价格描述'},
            share_price: {type: DECIMAL(10, 2), defaultValue: 0.00, comment: '分销价'},
            transportation_flag: {type: TINYINT, defaultValue: 0, comment: '是否包含大交通：1-是，0-否'},
            departure_city: {type: STRING(100), comment: '出发地'},
            destination_city: {type: STRING(100), comment: '目的地'},
            highlights: {type: TEXT, comment: '行程亮点'},
            details: {type: TEXT, comment: '产品详情'},
            cost_included: {type: TEXT, comment: '费用包含'},
            cost_excluded: {type: TEXT, comment: '费用不含'},
            refund_rules: {type: TEXT, comment: '退款规则'},
            booking_notice: {type: TEXT, comment: '预定须知'},
            hotel_levels: {type: TEXT, comment: '酒店级别配置，JSON格式存储，包含级别名称、价格、描述、单房差等信息'},
            sales: {type: BIGINT, defaultValue: 0, comment: '销量'},
            ...getDefaultModelConfig(app)
        },
        {
            tableName: 'travel',
            timestamps: true,
            createdAt: 'create_time',
            updatedAt: 'update_time'
        }
    );
    Travel.associate = function() {
        Travel.hasMany(app.model.Business.TravelSpec, { foreignKey: 'travel_id', sourceKey: 'id', as: 'specs' });
        // sku 关联 travel
        Travel.hasMany(app.model.Business.TravelSku, { foreignKey: 'travel_id', sourceKey: 'id', as: 'skus' });
        // 关联分类
        Travel.belongsTo(app.model.Business.TravelCategory, { foreignKey: 'category_id', sourceKey: 'id', as: 'category' });
        // 关联创建的系统用户
        Travel.belongsTo(app.model.System.SystemUser, { foreignKey: 'creator', sourceKey: 'id', as: 'creatorUser' });
    };
    return Travel;
};
